<?php

namespace App\Models;

class Collections extends Base
{
    protected $table = 'collections';

    protected $fillable = [
        'id','collect_id','user_id','store_id','collect_at','message','goods_price','type','created_at','updated_at'
    ];

    /**
     * 获取条数
     * @param Request $request
     * @return bool
     */
    protected function getCounts($params = [], $fields = ['*']){
        $counts = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['user_id']) && $params['user_id']) {
                    $query ->where('user_id',$params['user_id']);
                }
                if(isset($params['type']) && $params['type']) {
                    $query ->where('type',$params['type']);
                }
            })->count();
        return $counts;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = null, $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        if(isset($page)){
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['type']) && $params['type']) {
                        $query ->where('type',$params['type']);
                    }
                })->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        }else{
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['type']) && $params['type']) {
                        $query ->where('type',$params['type']);
                    }
                })->orderBy($sort, $orderby)->get();
        }
        return $lists;
    }
}
